package ui;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import common.Commands;
import common.commands.ICommand.CommandValue;


public class Terminal {
	
	public static String prompt = new String("Clac>");
	
	public static void Launch() {
		
		CommandValue ret;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try
    	{
    		System.out.print(prompt);
    		String s = br.readLine();
    		
    		while ((ret = Commands.RunCommand(s)) != CommandValue.EXIT) {
    			
    			if (ret == CommandValue.UNKNOWN) {
    				System.out.println("Unknown command: " + s);
    			}
    			
    			System.out.print(prompt);
        		s = br.readLine();
    		}	
    		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static boolean Launch(FileInputStream file) {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(file));
		
        String s;
		CommandValue ret;
        
        try {
			while ((s = br.readLine()) != null) {
			
				if ((ret = Commands.RunCommand(s)) == CommandValue.EXIT) {
					return false;
				}
				
				if (ret == CommandValue.UNKNOWN) {
    				System.out.println("Unknown command: " + s);
    			}
			}
		} catch (IOException e) {
			System.out.println("Error while reading script file.");
		}
        
        return true;
	}
}
